<?php

/**
 * skill_album actions.
 *
 * @package    jiazhang
 * @subpackage skill_album
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class skill_albumActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
        $page = $request->getParameter('page',1);
        $dql = Doctrine::getTable('SkillAlbum')->createQuery()->orderBy('created_at desc');
        $this->pager = new sfDoctrinePager('SkillAlbum',30);
        $this->pager->setQuery($dql);
        $this->pager->setPage($page);
        $this->pager->init();
  }
  
  public function executeAdd_skill(sfWebRequest $request){
    $this->forward404Unless($this->album = Doctrine::getTable('SkillAlbum')->findOneById((int)$request->getParameter('skill_album_id',0)));
      
    if($request->isMethod('POST')){
          $skill_id = (int)($request->getParameter('skill_id',0));
          $album_id = (int)$request->getParameter('skill_album_id',0);
          $sort_num = (int)$request->getParameter('sort_num',0);
          
          $album = new SkillAlbumIds();
          $album->setSkillAlbumId($album_id)->setSkillId($skill_id)->setSortNum($sort_num)->save();
          $this->getUser()->setFlash('success', '数据保存成功！');
          $this->redirect('skill_album/show?id='.$album_id);
    }
      
  }
  
  public function executeDelete_skill(sfWebRequest $request)
  {
          $skill_id = (int)($request->getParameter('skill_id',0));
          $album_id = (int)$request->getParameter('skill_album_id',0);
          Doctrine::getTable('SkillAlbumIds')->createQuery()->where('skill_album_id = ?',$album_id)->andWhere('skill_id = ?',$skill_id)->execute()->delete();
          $this->getUser()->setFlash('success', '数据删除成功！');
          $this->redirect('skill_album/show?id='.$album_id);
  }
  
  public function executeShow(sfWebRequest $request)
  {
      $album_id = (int)$request->getParameter('id',0);
      $this->skills = Doctrine::getTable('SkillAlbumIds')->createQuery()->where('skill_album_id = ?',$album_id)->orderBy('created_at desc')->execute();
  }
  
  public function executeAdd(sfWebRequest $request){
      
  }
  
  public function executeEdit(sfWebRequest $request){
      $this->forward404Unless($this->album = Doctrine::getTable('SkillAlbum')->findOneById((int)$request->getParameter('id',0)));
      
  }
  
  
  public function executeCreate(sfWebRequest $request)
  {
    if($request->isMethod('POST')){
          $title = trim($request->getParameter('title',''));
          $url = $request->getParameter('descript','');
          $cover = $request->getFiles('cover');
          $display = (int)$request->getParameter('display',0);
          $id = (int)$request->getParameter('id',0);
          
          if($id > 0){
              $skill_album = Doctrine::getTable('SkillAlbum')->findOneById($id);
          }else{
              $skill_album = new SkillAlbum();
          }
          
          $is_save = $skill_album->setTitle($title)->setDescript($url)->setDisplay($display)->trySave();
          
          if($is_save){
              
              
              if($cover['error'] == 0){
              
                    $file_name = $cover['name'];
                    $file_ext_tmp = explode('.', $file_name);
                    $file_ext = $file_ext_tmp[count($file_ext_tmp)-1];
                    $allow_file_type = array('jpg','bmp','png','gif','jpeg');

                    if(in_array($file_ext, $allow_file_type)){
                        $year = date('Y',time());
                        $month = date('m',time());
                        $day = date('d',time());
                        $save_file_name = $year.'_'.$month.'_'.$day.'_'.substr(md5(time()+rand(0,999)),25).'.'.$file_ext;
                        $upyun_save_path = '/skill_album/'.$year.'/'.$month.'/'.$save_file_name;
                        $upyun = new UpYun(sfConfig::get('app_upyun_space'),sfConfig::get('app_upyun_user') , sfConfig::get('app_upyun_pwd'));
                        $image_url=  '';

                        try {
                            $fh = fopen($cover['tmp_name'], 'rb');
                            $rsp = $upyun->writeFile($upyun_save_path, $fh, True);   // 上传图片，自动创建目录
                            $image_url=  sfConfig::get('app_img_url').$upyun_save_path;
                            fclose($fh);
                        }
                        catch(Exception $e) {
                            echo $e->getCode();
                            echo $e->getMessage();
                        }
                    }else{
                        $this->getUser()->setFlash('error', '图片类型错误！');
                        $this->redirect('skill_album/add');
                    }
                   $skill_album->setCover($image_url)->save();  
              }
             
              $this->getUser()->setFlash('success', '数据保存成功！');
          }
          
          $this->redirect('skill_album/index');
    }
  }
}
